package ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.GS;

/**
 * Preferences dialog.
 * @author TKington
 *
 */
public class SettingsDialog extends javax.swing.JDialog {
	/** Use a proxy? */
    private JCheckBox useProxyCB;
    /** Proxy host. */
    private JTextField proxyHostTF;
    /** Proxy port. */
    private JTextField proxyPortTF;
    /** Proxy host label. */
    private JLabel hostLB;
    /** Proxy port label. */
    private JLabel portLB;
    /** SGF editor. */
    private JTextField sgfTF;
    
    /**
     * Creates SettingsDialog.
     * @param parent the parent Frame
     */
    public SettingsDialog(java.awt.Frame parent) {
        super(parent, true);
        setTitle("GoViewer Settings");
        Container cp = getContentPane();

        Box topPanel = new Box(BoxLayout.Y_AXIS);
        
        showGhostCB = new javax.swing.JCheckBox();
        showGhostCB.setText("Display transparent stone at cursor position");
        topPanel.add(showGhostCB);

        clickSoundCB = new javax.swing.JCheckBox();
        clickSoundCB.setText("Enable stone sound");
        topPanel.add(clickSoundCB);

        soundCB = new javax.swing.JCheckBox();
        soundCB.setText("Enable right/wrong sounds");
        topPanel.add(soundCB);

        topPanel.add(Box.createVerticalStrut(10));
        
        Box box = new Box(BoxLayout.X_AXIS);
        box.add(Box.createHorizontalStrut(5));
        box.add(new JLabel("SGF Editor:"));
        sgfTF = new JTextField();
        box.add(sgfTF);
        JButton browse = new JButton("Browse...");
        browse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onBrowse();
			}
		});
        box.add(browse);
        box.add(Box.createHorizontalStrut(10));
        topPanel.add(box);
        box.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        topPanel.add(Box.createVerticalStrut(5));
        
        checkUpdateCB = new javax.swing.JCheckBox();
        checkUpdateCB.setText("Check for newer version at startup");
        checkUpdateCB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableProxyFields();
            }
        });
        topPanel.add(checkUpdateCB);

        JPanel buttonPanel = new javax.swing.JPanel();
        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        JButton okButton = new javax.swing.JButton();
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onOK();
            }
        });

        buttonPanel.add(okButton);
        
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        
        JPanel proxyPanel = new JPanel(new GridBagLayout());
        
        GridBagConstraints c = new GridBagConstraints();
        
        c.gridwidth = 2;
        c.anchor = GridBagConstraints.WEST;
        useProxyCB = new JCheckBox("Use proxy to check for newer version");
        useProxyCB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableProxyFields();
            }
        });
        proxyPanel.add(useProxyCB, c);
        
        hostLB = new JLabel("Proxy Host:");
        c.gridwidth = 1;
        c.gridy = 1;
        c.insets = new Insets(0, 10, 0, 0);
        proxyPanel.add(hostLB, c);
        
        proxyHostTF = new JTextField(15);
        c.gridx = 1;
        c.insets = new Insets(0, 0, 0, 0);
        proxyPanel.add(proxyHostTF, c);
        
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(0, 10, 0, 0);
        portLB = new JLabel("Proxy Port:");
        proxyPanel.add(portLB, c);
        
        c.gridx = 1;
        c.insets = new Insets(0, 0, 0, 0);
        proxyPortTF = new JTextField(15);
        proxyPanel.add(proxyPortTF, c);
        
        cp.add(topPanel, java.awt.BorderLayout.NORTH);
        
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.setBorder(BorderFactory.createTitledBorder("Proxy Settings"));
        panel.add(proxyPanel);
        
        cp.add(panel, BorderLayout.CENTER);
        
        cp.add(buttonPanel, java.awt.BorderLayout.SOUTH);
        
        showGhostCB.setSelected(GS.getShowGhost());
        clickSoundCB.setSelected(GS.getClickSoundEnabled());
        soundCB.setSelected(GS.getSoundEnabled());
        checkUpdateCB.setSelected(GS.getCheckForUpdates());
        useProxyCB.setSelected(GS.getUseProxy());
        proxyHostTF.setText(GS.getProxyHost());
        proxyPortTF.setText(String.valueOf(GS.getProxyPort()));
        File sgf = GS.getSGFEditor();
        if(sgf != null)
        	sgfTF.setText(sgf.getAbsolutePath());

        enableProxyFields();
        pack();
        setLocationRelativeTo(parent);
        setVisible(true);
    }
    
    /** Browse for an SGF editor. */
    private void onBrowse() {
    	JFileChooser choose = new JFileChooser();
    	choose.setDialogTitle("Select SGF Editor");
    	if(choose.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
    		sgfTF.setText(choose.getSelectedFile().getAbsolutePath());
    	}
    }
    
    /** Enable/disable proxy-related fields. */
    private void enableProxyFields() {
        boolean check = checkUpdateCB.isSelected();
        boolean use = useProxyCB.isSelected();
        useProxyCB.setEnabled(check);
        hostLB.setEnabled(check && use);
        portLB.setEnabled(check && use);
        proxyHostTF.setEnabled(check && use);
        proxyPortTF.setEnabled(check && use);
    }

    /** Saves changes when user presses OK. */
    private void onOK() {
        boolean use = useProxyCB.isSelected();
        if(use) {
            int port = 0;
            try {
                port = Integer.parseInt(proxyPortTF.getText());
            }
            catch(NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "Invalid port");
                return;
            }
            
            GS.setProxyPort(port);
            GS.setProxyHost(proxyHostTF.getText());
        }
        
        GS.setUseProxy(use);
        GS.setShowGhost(showGhostCB.isSelected());
        GS.setClickSoundEnabled(clickSoundCB.isSelected());
        GS.setSoundEnabled(soundCB.isSelected());
        GS.setCheckForUpdates(checkUpdateCB.isSelected());
        String sgf = sgfTF.getText();
        if(sgf.length() > 0)
        	GS.setSGFEditor(new File(sgf));

        setVisible(false);
    }

    /** Check for updates? */
    private javax.swing.JCheckBox checkUpdateCB;
    /** Click sound when stone is played? */
    private javax.swing.JCheckBox clickSoundCB;
    /** Display ghost stone? */
    private javax.swing.JCheckBox showGhostCB;
    /** Play right/wrong sounds? */
    private javax.swing.JCheckBox soundCB;

}
